﻿using SD.IOC.Core.Mediators;
using SD.Toolkits.Excel;
using SlamDunk.MES.DataDrive.Bussiness.Models.MaintenanceContext;
using SlamDunk.MES.DataDrive.Common;
using SlamDunk.MES.DataDrive.Common.Attributes;
using SlamDunk.MES.IAppService.Interfaces;
using System;
using System.Linq;

namespace SlamDunk.MES.DataDrive.Bussiness.Importers
{
    /// <summary>
    /// 导入维护数据
    /// </summary>
    [DataTag(18, "维护数据", "MES维护数据", DataDriveConfig.RealgoalName, DataDriveConfig.RealgoalConnectionStrings)]
    public class ImportMaintenance
    {
        #region # 导入设备资产 —— void ImportEquipmentAssets()
        /// <summary>
        /// 导入设备资产
        /// </summary>
        [DataTag("导入设备资产")]
        public void ImportEquipmentAssets()
        {
            IFoundationContract foundationContract = ResolveMediator.Resolve<IFoundationContract>();
            IResourceContract resourceContract = ResolveMediator.Resolve<IResourceContract>();
            IMaintenanceContract maintenanceContract = ResolveMediator.Resolve<IMaintenanceContract>();
            EquipmentAsset[] equipmentAssets = ExcelReader.ReadFile<EquipmentAsset>(SystemConfig.InitalForMaintenanceDataPath, "设备资产");

            if (ImportCacheData.FacilityInfos == null)
            {
                ImportCacheData.FacilityInfos = foundationContract.GetFacilities(null, null, null, null).ToList();
            }
            if (ImportCacheData.EquipmentInfos == null)
            {
                ImportCacheData.EquipmentInfos = resourceContract.GetEquipmentsByPage(null, null, null, 1, int.MaxValue).Datas.ToList();
            }
            foreach (EquipmentAsset equipmentAsset in equipmentAssets)
            {
                Guid? facilityId = ImportCacheData.FacilityInfos.FirstOrDefault(x => x.Number == equipmentAsset.工厂模型编号)?.Id;
                Guid equipmentId = ImportCacheData.EquipmentInfos.Single(x => x.Number == equipmentAsset.设备编号).Id;

                maintenanceContract.CreateEquipmentAsset(equipmentAsset.设备资产编号, equipmentAsset.设备资产名称, equipmentId, equipmentAsset.生产日期, equipmentAsset.采购日期, equipmentAsset.启用日期, equipmentAsset.制造商, equipmentAsset.规格, equipmentAsset.型号, equipmentAsset.价值, equipmentAsset.使用年限, equipmentAsset.折旧年限, equipmentAsset.售后电话, facilityId, equipmentAsset.负责人, equipmentAsset.描述);
            }
        }
        #endregion

        #region # 导入设备维护类 —— void ImportEquipmentMaintainClasses()
        /// <summary>
        /// 导入设备维护类
        /// </summary>
        [DataTag("导入设备维护类")]
        public void ImportEquipmentMaintainClasses()
        {
            IFoundationContract foundationContract = ResolveMediator.Resolve<IFoundationContract>();
            IMaintenanceContract maintenanceContract = ResolveMediator.Resolve<IMaintenanceContract>();
            EquipmentMaintainClass[] maintainClasses = ExcelReader.ReadFile<EquipmentMaintainClass>(SystemConfig.InitalForMaintenanceDataPath, "设备维护类");

            if (ImportCacheData.FacilityInfos == null)
            {
                ImportCacheData.FacilityInfos = foundationContract.GetFacilities(null, null, null, null).ToList();
            }
            foreach (EquipmentMaintainClass maintainClass in maintainClasses)
            {
                Guid? facilityId = ImportCacheData.FacilityInfos.FirstOrDefault(x => x.Number == maintainClass.工厂模型编号)?.Id;

                maintenanceContract.CreateEquipmentMaintainClass(maintainClass.设备维护类编号, maintainClass.设备维护类名称, maintainClass.维护内容, maintainClass.维护周期, maintainClass.维护负责人, facilityId, maintainClass.描述);
            }
        }
        #endregion

        #region # 导入设备维护记录 —— void ImportEquipmentMaintenances()
        /// <summary>
        /// 导入设备维护记录
        /// </summary>
        [DataTag("导入设备维护记录")]
        public void ImportEquipmentMaintenances()
        {
            IFoundationContract foundationContract = ResolveMediator.Resolve<IFoundationContract>();
            IMaintenanceContract maintenanceContract = ResolveMediator.Resolve<IMaintenanceContract>();
            EquipmentMaintenance[] equipmentMaintenances = ExcelReader.ReadFile<EquipmentMaintenance>(SystemConfig.InitalForMaintenanceDataPath, "设备维护记录");

            if (ImportCacheData.FacilityInfos == null)
            {
                ImportCacheData.FacilityInfos = foundationContract.GetFacilities(null, null, null, null).ToList();
            }
            if (ImportCacheData.EquipmentMaintainClassInfos == null)
            {
                ImportCacheData.EquipmentMaintainClassInfos = maintenanceContract.GetEquipmentMaintainClassesByPage(null, null, 1, int.MaxValue).Datas.ToList();
            }
            if (ImportCacheData.EquipmentAssetInfos == null)
            {
                ImportCacheData.EquipmentAssetInfos = maintenanceContract.GetEquipmentAssetsByPage(null, null, null, null, 1, int.MaxValue).Datas.ToList();
            }

            foreach (EquipmentMaintenance equipmentMaintenance in equipmentMaintenances)
            {
                Guid? facilityId = ImportCacheData.FacilityInfos.FirstOrDefault(x => x.Number == equipmentMaintenance.工厂模型编号)?.Id;
                Guid maintenanceClassId = ImportCacheData.EquipmentMaintainClassInfos.Single(x => x.Number == equipmentMaintenance.设备维护类编号).Id;
                Guid equipmentAssetId = ImportCacheData.EquipmentAssetInfos.Single(x => x.Number == equipmentMaintenance.设备资产编号).Id;

                maintenanceContract.CreateEquipmentMaintenance(equipmentMaintenance.维护记录编号, maintenanceClassId, equipmentAssetId, equipmentMaintenance.维护日期, equipmentMaintenance.维护工时, equipmentMaintenance.维护部位, equipmentMaintenance.维护内容, equipmentMaintenance.备件使用, equipmentMaintenance.维护人, facilityId, equipmentMaintenance.描述);
            }
        }
        #endregion
    }
}
